package com.example.app.service;

import com.example.app.entity.GoodItem;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class GoodService {
    private Map<Long, GoodItem> goods = Collections.synchronizedMap(new HashMap<Long, GoodItem>());

    public GoodItem addItem(GoodItem item)
    {
        goods.put(item.getId(),item);
        return item;
    }

    public GoodItem getItem(Long id)
    {
        return goods.get(id);
    }

    public List<GoodItem> findItems(Integer num, Boolean sold)
    {
        List<GoodItem> result =new ArrayList<>();
        for (GoodItem item:goods.values())
        {
            if (num != null && !item.getNum().equals(num))
            {
                continue;
            }
            if (sold!=null && !sold.equals(item.getSold())) {
                continue;
            }
            result.add(item);
        }
        return result;
    }

    public void updateItem(Long id, GoodItem good)
    {
        GoodItem item = goods.get(id);
        item.setNum(good.getNum());
        item.setSold(good.getSold());
        goods.put(id, item);
    }

    public void deleteItem(Long id)
    {
        goods.remove(id);
    }
}
